#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int paint_fence(int a, int b, int c, int d) {
    int fence = 0;

    if(c>a && d < b){
        fence = b - a;
    }
    if(a > c && b < d){
        fence = d - c;
    }
    if(c < a && d > a && d < b){
        fence = b - c;
    }
    if(a < c && b > c && b < d){
        fence = d - a;
    }
    if(c > b){
        fence = b -a + d - c;
    }
    if(a > d){
        fence = b -a + d - c;
    }
    return fence;
}

int main(int argc, char const *argv[])
{
    int ret;
    ret = paint_fence(3, 5, 6, 8);
    cout<<ret<<endl;
    return 0;
}
